package com.samsung.spen.sshare.explorer.util;

import java.util.ArrayList;

import com.samsung.spen.sshare.R;

public class FileUtil {

	public static FileUtil instance = null;

	public static final String[] music = { "mp3", "midi", "wma", "ogg", "flac","mp2" };
	public static final String[] video = { "mp4", "wmv", "flv" };
	public static final String[] word = { "doc", "docx" };
	public static final String[] excel = { "xls", "xlsx" };
	public static final String[] rar = { "rar", "zip" };
	public static final String[] system = { "d", "bat", "cfg", "ini" };
	public static final String[] text = { "txt", "text", "h", "cpp", "c" };
	public static final String[] image = { "jpg", "jpeg", "png", "gif", "psd" };
	public static final String[] apk = { "apk", "exe", "bat" };
	public static final String[] net = { "xml", "yml", "kml", "html", "xhtml",
			"xaml", "json", "js", "css" };
	public static final String[] pdf = { "pdf" };

	ArrayList<DataResource> resources = new ArrayList<FileUtil.DataResource>();

	public FileUtil() {
		resources.add(new DataResource(R.drawable.exp_file_music, music));
		resources.add(new DataResource(R.drawable.exp_file_video, video));
		resources.add(new DataResource(R.drawable.exp_file_text, word));
		resources.add(new DataResource(R.drawable.exp_file_text, excel));
		resources.add(new DataResource(R.drawable.exp_file_text, rar));
		resources.add(new DataResource(R.drawable.exp_file_text, system));
		resources.add(new DataResource(R.drawable.exp_file_text, text));
		resources.add(new DataResource(R.drawable.exp_file_image, image));
		resources.add(new DataResource(R.drawable.exp_file_text, apk));
		resources.add(new DataResource(R.drawable.exp_file_text, net));
		resources.add(new DataResource(R.drawable.exp_file_text, pdf));
	}

	public static FileUtil getInstance() {
		
		if (instance == null) {
			instance = new FileUtil();
		}
		return instance;
	}

	public int getReourceFile(String externalFile) {

		for (DataResource item : resources) {

			for (String str : item.extFile) {

				if (str.equalsIgnoreCase(externalFile)) {
					return item.idResource;
				}
			}
		}
		return R.drawable.exp_file_blank;
	}

//	public void openFile(Context context, File file) {
//
//		String title = file.getName();
//		int index = title.lastIndexOf(".");
//		String extStr = title.substring(index + 1);
//
//		DataResource data = null;
//
//		for (DataResource item : resources) {
//
//			for (String str : item.extFile) {
//
//				if (str.equalsIgnoreCase(extStr)) {
//					data = item;
//				}
//			}
//		}
//
//		if (data != null) {
//
//			boolean canOpen = true;
//			Intent intent = new Intent();
//			intent.setAction(android.content.Intent.ACTION_VIEW);
//
//			if (data.idResource == R.drawable.file_music) {
//
//				intent.setDataAndType(Uri.fromFile(file), "audio/*");
//
//			} else if (data.idResource == R.drawable.file_video) {
//
//				intent.setDataAndType(Uri.fromFile(file), "video/*");
//
//			} else if (data.idResource == R.drawable.file_text) {
//
//				intent.setDataAndType(Uri.fromFile(file), "text/*");
//
//			} else if (data.idResource == R.drawable.file_zip) {
//
//				intent.setDataAndType(Uri.fromFile(file), "zip/*");
//
//			} else if (data.idResource == R.drawable.file_apk) {
//
//				intent.setDataAndType(Uri.fromFile(file), "application/*");
//
//			} else if (data.idResource == R.drawable.file_image) {
//
//				intent.setDataAndType(Uri.fromFile(file), "image/*");
//
//			} else if (data.idResource == R.drawable.file_pdf) {
//
//				intent.setDataAndType(Uri.fromFile(file), "application/pdf");
//
//			} else if (data.idResource == R.drawable.file_word) {
//
//				intent.setDataAndType(Uri.fromFile(file), "application/word");
//
//			} else if (data.idResource == R.drawable.file_excel) {
//
//				intent.setDataAndType(Uri.fromFile(file), "application/excel");
//
//			} else if (data.idResource == R.drawable.file_net) {
//
//				intent.setDataAndType(Uri.fromFile(file), "text/*");
//
//			} else {
//				// canOpen=false;
//				intent.setDataAndType(Uri.fromFile(file), "text/*");
//			}
//
//			if (canOpen) {
//
//				try {
//
//					context.startActivity(intent);
//					return;
//				} catch (ActivityNotFoundException e) {
//				}
//			}
//
//			Toast.makeText(context, "Can not open file", Toast.LENGTH_SHORT)
//					.show();
//
//		} else {
//
//			Intent intent = new Intent();
//			intent.setAction(android.content.Intent.ACTION_VIEW);
//			intent.setDataAndType(Uri.fromFile(file), "text/*");
//			context.startActivity(intent);
//
//		}
//
//	}
	
	class DataResource {

		public int idResource;
		public String[] extFile;

		public DataResource(int idResource, String[] extFile) {
			this.idResource = idResource;
			this.extFile = extFile;
		}
	}
}
